package com.shd.edanpei.presenter;

import android.os.Bundle;

import com.shd.edanpei.ui.activity.MainActivity;
import com.shd.edanpei.R;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.ui.activity.CertificationActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.RoleBean;
import com.shd.edanpei.presenter.contract.RoleChoiceContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.RoleEnum;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/10/19.
 * yang-wen-wen
 */

public class RoleChoicePresenter extends BasePresenterImpl<RoleChoiceContract.RoleChoiceIView>
        implements RoleChoiceContract.RoleChoicIPresenter {

    private RoleBean mBean;
    private boolean canSubmit = false;

    public RoleChoicePresenter(RoleChoiceContract.RoleChoiceIView mView) {
        super(mView);
    }

    public void initRoleData() {
        List<RoleBean> list = new ArrayList<>();
        list.add(new RoleBean(RoleEnum.other.getId(), RoleEnum.other.getDes(), R.drawable.cho_contact_unselected, R.drawable.cho_contact_selected, false));
        list.add(new RoleBean(RoleEnum.bank.getId(), RoleEnum.bank.getDes(), R.drawable.cho_manage_unselected, R.drawable.cho_manage_selected, false));
        mBaseIView.getAdapter().setNewData(list);
    }

    @Override
    public void setData(RoleBean bean) {
        mBean = bean;
        checkData();
    }

    @Override
    public void submit() {
        if (canSubmit) {
            DialogUtil.sureRole(mBaseIView.getContext(), mBean.getRoleName(), new DialogUtil.SureListener() {
                @Override
                public void sure() {
                    ApiCreator.getApi().selectRole(mBean.getRoleID())
                            .compose(RxTransformer.<NoDataResponse>errorHandle())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(new BaseObserver<NoDataResponse>() {
                                @Override
                                protected void onSuccess(NoDataResponse data) {
                                    if (mBean.getRoleID() == RoleEnum.other.getId()) {
                                        mBaseIView.startActivity(MainActivity.class);
                                    } else {
                                        DaoManager.getInstance().setRole(mBean.getRoleID());
                                        //进入实名认证
                                        Bundle bundle = new Bundle();
                                        bundle.putInt(Constant.FROM, CertificationActivity.FROM_LOGIN);
                                        mBaseIView.startActivity(CertificationActivity.class, bundle);
                                    }
                                    DaoManager.getInstance().setRole(mBean.getRoleID());
                                    mBaseIView.getContext().finish();
                                }
                            });
                }
            });


        }
    }

    private void checkData() {
        if (mBean != null) {
            canSubmit = true;
        } else {
            canSubmit = false;
        }
    }

}
